﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EasyClassUI.forms.blog;
using EasyClassUI.WebRequest;
using EasyClassUI.entity;
using System.Threading.Tasks;

namespace EasyClassUI.forms.group
{
    public partial class groupUC : UserControl
    {
        public static groupUC groupView;

        public groupUC()
        {
            InitializeComponent();
            createGButton.Visible = false;
            addGButton.Visible = false;
            myAddedGroupPanel.BackColor = Color.Lavender;
            myCreatedGroupPanel.BackColor = Color.Transparent;
            showAllMyAddedGs();
            showNewestBlogs();
        }
        public bool isDefaultState = true;  //即没搜索之前，panel显示所有

        public void CustomDisplayGroup(List<StudyGroup> studyGroups) {
            groupShowPanel.Controls.Clear();
            foreach (StudyGroup group in studyGroups) {
                GroupProfile gp = new GroupProfile(group.StudyGroupName, group.Category, group, false);
                groupShowPanel.Controls.Add(gp);
            }
        }

        public void DisplayGroup(StudyGroup group) {
            GroupProfile gp = new GroupProfile(group.StudyGroupName, group.Category, group, false);
            groupShowPanel.Controls.Add(gp);
        }

        public void showAllMyAddedGs()
        {
            groupShowPanel.Controls.Clear();
            GroupService groupService = new GroupService();
            QueryGroupStatus groups = groupService.QueryWhichGroupHasBeenJoin(User.CurrentUser.Token);
            StudyGroup.GroupIJoined = groups.Groups;
            var groupICreated = StudyGroup.GroupIJoined.Where(p => p.User.UserAccount == User.CurrentUser.UserAccount);
            StudyGroup.GroupICreated = groupICreated.ToList();
            StudyGroup.LocalGroupDataGenerated = true;
            foreach (StudyGroup group in StudyGroup.GroupIJoined) {
                GroupProfile gp = new GroupProfile(group.StudyGroupName, group.Category, group, false);
                groupShowPanel.Controls.Add(gp);
            }
        }

        public void DisplayPost(Post post) {
            Post.loaclPosts.Add(post);
            BlogInShort bis = new BlogInShort(post.Title, post.Category, post.CreateTime.ToString(), post); 
            groupBlogInShortPanel.Controls.Add(bis);
        }

        public void showNewestBlogs()
        {
            groupBlogInShortPanel.Controls.Clear();
            PostService postService = new PostService();
            PostDownloadStatus result = postService.SearchNPostsInGroupsImIn(4, User.CurrentUser.Token);
            foreach (Post post in result.Posts) {
                Post.loaclPosts.Add(post);
                BlogInShort bis = new BlogInShort(post.Title, post.Category, post.CreateTime.ToString(), post);
                groupBlogInShortPanel.Controls.Add(bis);
            }
        }

        public static explicit operator groupUC(Form v) {
            throw new NotImplementedException();
        }

        //搜索
        private void searchGroupButton_Click(object sender, EventArgs e)
        {
            isDefaultState = false;
            List<StudyGroup> result = StudyGroup.GroupIJoined.Where(p => p.StudyGroupName.Contains(searchTextBox.Text.ToLower()) || p.StudyGroupName.Contains(searchTextBox.Text.ToUpper())).ToList();
            CustomDisplayGroup(result);
        }

        //显示/隐藏加小组的两个按钮
        private void addButton_Click(object sender, EventArgs e)
        {
            createGButton.Visible = !createGButton.Visible;
            addGButton.Visible = !addGButton.Visible;
        }

        private void myAddedGroupPanel_MouseDown(object sender, MouseEventArgs e)
        {
            myAddedGroupPanel.BackColor = Color.Lavender;
            myCreatedGroupPanel.BackColor = Color.Transparent;
        }

        private void myCreatedGroupPanel_MouseDown(object sender, MouseEventArgs e)
        {
            myAddedGroupPanel.BackColor = Color.Transparent;
            myCreatedGroupPanel.BackColor = Color.Lavender;
        }

        private void createGButton_Click(object sender, EventArgs e)
        {
            CreateNewGroup cnp = new CreateNewGroup();
            cnp.Show();
        }

        private void addGButton_Click(object sender, EventArgs e)
        {
            AddToGroup.addToGroup = new AddToGroup();
            AddToGroup.addToGroup.Show();
        }

        private void myCreatedGroupPanel_Click(object sender, EventArgs e)
        {
            myCreatedGroupPanel.BackColor = Color.Lavender;
            myAddedGroupPanel.BackColor = Color.Transparent;
            //显示所有我创建的组（注意default和非default分情况讨论）
            groupShowPanel.Controls.Clear();
            if (!StudyGroup.LocalGroupDataGenerated) {
                var groupICreated = StudyGroup.GroupIJoined.Where(p => p.User.UserAccount == User.CurrentUser.UserAccount);
                StudyGroup.GroupICreated = groupICreated.ToList();
                StudyGroup.LocalGroupDataGenerated = true;
            }

            foreach (StudyGroup group in StudyGroup.GroupICreated) {
                GroupProfile gp = new GroupProfile(group.StudyGroupName, group.Category, group, false);
                groupShowPanel.Controls.Add(gp);
            }
        }

        private void myAddedGroupPanel_Click(object sender, EventArgs e)
        {
            myCreatedGroupPanel.BackColor = Color.Transparent;
            myAddedGroupPanel.BackColor = Color.Lavender;
            //显示所有我加入的组（注意default和非default分情况讨论）
            groupShowPanel.Controls.Clear();
            foreach (StudyGroup group in StudyGroup.GroupIJoined) {
                GroupProfile gp = new GroupProfile(group.StudyGroupName, group.Category, group, false);
                groupShowPanel.Controls.Add(gp);
            }
        }

        private void myAddedGroupLabel_Click(object sender, EventArgs e)
        {
            myCreatedGroupPanel.BackColor = Color.Transparent;
            myAddedGroupPanel.BackColor = Color.Lavender;
            //显示所有我加入的组（注意default和非default分情况讨论）
            groupShowPanel.Controls.Clear();
            foreach (StudyGroup group in StudyGroup.GroupIJoined) {
                GroupProfile gp = new GroupProfile(group.StudyGroupName, group.Category, group, false);
                groupShowPanel.Controls.Add(gp);
            }
        }

        private void myCreatedGroupLabel_Click(object sender, EventArgs e)
        {
            myCreatedGroupPanel.BackColor = Color.Lavender;
            myAddedGroupPanel.BackColor = Color.Transparent;
            //显示所有我创建的组（注意default和非default分情况讨论）
            groupShowPanel.Controls.Clear();
            if (!StudyGroup.LocalGroupDataGenerated) {
                var groupICreated = StudyGroup.GroupIJoined.Where(p => p.User.UserAccount == User.CurrentUser.UserAccount);
                StudyGroup.GroupICreated = groupICreated.ToList();
                StudyGroup.LocalGroupDataGenerated = true;
            }

            foreach (StudyGroup group in StudyGroup.GroupICreated) {
                GroupProfile gp = new GroupProfile(group.StudyGroupName, group.Category, group, false);
                groupShowPanel.Controls.Add(gp);
            }
        }
    }
}
